package com.car.service.impl.yearcheck;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.dao.user.UserDao;
import com.car.dao.yearcheck.YearCheckMapper;
import com.car.entity.User;
import com.car.entity.YearCheck;
import com.car.service.yearcheck.YearCheckService;
import com.car.util.ResultMsg;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class YearCheckServiceImpl implements YearCheckService {

    @Getter
    @Setter
    @Autowired
    YearCheckMapper yearCheckMapper;

    @Autowired
    UserDao userDao;

    @Override
    public Page selectLikeYearCheck(Map<String, Object> map) {
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        Page page = new Page((pageNum - 1) * pageSize, pageSize);

        Integer pageIndex = (pageNum - 1) * pageSize;
        map.put("pageIndex", pageIndex);
        Integer maintainCount = yearCheckMapper.getYearCheckCount(map);
        //设置起始位置
        List list = yearCheckMapper.selectLikeYearCheck(map);
        //先设置总条数
        page.setTotal(maintainCount);
        //设置每页大小
        page.setSize(pageSize);
        //设置当前页
        page.setCurrent(pageNum);
        //设置内容
        page.setRecords(list);
        return page;
    }

    @Override
    public ResultMsg addYearCheck(YearCheck yearCheck, String userName) {
        ResultMsg resultMsg = new ResultMsg();
        User addUser = userDao.getUserByName(userName);
        yearCheck.setUserId(addUser.getUserId());
        int flag = yearCheckMapper.insert(yearCheck);
        if (flag > 0) {
            resultMsg.setMsg("添加成功");
        } else {
            resultMsg.setMsg("添加失败");
        }
        resultMsg.setFlag(flag);
        return resultMsg;
    }

    @Override
    public ResultMsg delYearCheck(YearCheck yearCheck) {
        ResultMsg resultMsg = new ResultMsg();
        LambdaUpdateWrapper<YearCheck> wrapper=new LambdaUpdateWrapper();
        wrapper.eq(YearCheck::getYearCheckId,yearCheck.getYearCheckId());
        wrapper.set(YearCheck::getState,yearCheck.getState());
        int flag = yearCheckMapper.update(null,wrapper);
        if (flag > 0) {
            resultMsg.setMsg("删除成功");
        } else {
            resultMsg.setMsg("删除失败");
        }
        resultMsg.setFlag(flag);
        return resultMsg;
    }

    @Override
    public ResultMsg updateYearCheck(YearCheck yearCheck) {
        ResultMsg resultMsg = new ResultMsg();
        int flag = yearCheckMapper.updateById(yearCheck);
        if (flag > 0) {
            resultMsg.setMsg("修改成功");
        } else {
            resultMsg.setMsg("修改失败");
        }
        resultMsg.setFlag(flag);
        return resultMsg;
    }

    @Override
    public ResultMsg selectById(YearCheck yearCheck) {
        ResultMsg resultMsg = new ResultMsg();
        YearCheck result = yearCheckMapper.getYearCheckById(yearCheck);
        if (result != null) {
            resultMsg.setMsg("查询成功");
            resultMsg.setFlag(1);
            resultMsg.setEntityObj(result);
        } else {
            resultMsg.setMsg("查询失败");
            resultMsg.setFlag(0);
        }
        return resultMsg;
    }


}
